// img
import originalOrder from '@/assets/originalOrder.png';
import adjustOrder from '@/assets/adjustOrder.png';
import hedgingOrder from '@/assets/hedgingOrder.png';
import retorOrder from '@/assets/retorOrder.png';
import splitOrder from '@/assets/splitOrder.png';
import newOrder from '@/assets/newOrder.png';
import makeupOrder from '@/assets/makeupOrder.png';
// utils
import { REPORTS_MODEL, RETURN_TYPE, EXPORT_API } from '@/utils/const/returnedMoney';
// api
import { getPayOption, getDepartmentList, platformList } from '@/api/select';
import { getDepartmentAll } from '@/api/config';
import { getOrderSetting } from '@/api/order';

export default {
  data() {
    return {
      title: '',
      amountText: '', // 导出文件名
      exportApi: '', // 导出api
      deptTreeList: [], // 渠道订单-所属部门
      orderPlatformOption: [], // 渠道订单-下单平台
      financeListSetting: [], // 财务流水-自定义列数据
      departmentList: [], // 财务流水-所属部门
      payOption: [] // 财务流水-付款方式
    };
  },
  computed: {
    orderTypeImg() { // 订单类型
      return [{
        type: 0,
        img: originalOrder,
        bg: '#EFF4FF',
        color: '#406EFF'
      }, {
        type: 1,
        img: splitOrder,
        bg: '#E2F8F2',
        color: '#31CC9C'
      }, {
        type: 2,
        img: hedgingOrder,
        bg: '#FFEFF0',
        color: '#FF4E5F'
      }, {
        type: 3,
        img: newOrder,
        bg: '#F9ECFF',
        color: '#9B5BED'
      }, {
        type: 4,
        img: retorOrder,
        bg: '#FFF8EA',
        color: '#FFBC3D'
      }, {
        type: 5,
        img: adjustOrder,
        bg: '#FFF2EB',
        color: '#FD8546'
      }, {
        type: 6,
        img: originalOrder,
        bg: '#FDEFE9',
        color: '#FD8546'
      }, {
        type: 7,
        img: makeupOrder,
        bg: '#FFE6F2',
        color: '#F861AD'
      }];
    },
    reviewState() {
      return [{
        status: 0,
        reviewColor: '#31CC9C'
      }, {
        status: 1,
        reviewColor: '#406EFF'
      }, {
        status: 2,
        reviewColor: '#FD8546'
      }];
    },
    channelRangeKeys() {
      return [{
        attr: 'orderTime',
        start: 'orderStartTime',
        end: 'orderEndTime',
        type: 'time'
      }, {
        attr: 'taxAmount',
        start: 'taxAmountStart',
        end: 'taxAmountEnd',
        type: 'moneyFixed'
      }];
    },
    saleRangeKeys() {
      return [{
        attr: 'taxAmount',
        start: 'taxAmountStart',
        end: 'taxAmountEnd',
        type: 'moneyFixed'
      }, {
        attr: 'writeOffAmount',
        start: 'writeOffAmountStart',
        end: 'writeOffAmountEnd',
        type: 'moneyFixed'
      }, {
        attr: 'unWriteOffAmount',
        start: 'unWriteOffAmountStart',
        end: 'unWriteOffAmountEnd',
        type: 'moneyFixed'
      }];
    },
    financeRangeKeys() {
      return [{
        attr: 'incomeAmount',
        start: 'incomeAmountStart',
        end: 'incomeAmountEnd',
        type: 'moneyFixed'
      }, {
        attr: 'chargeAmount',
        start: 'chargeAmountStart',
        end: 'chargeAmountEnd',
        type: 'moneyFixed'
      }];
    },
    dateDataAttrName() {
      return [{
        attr: 'createTime',
        start: 'createStart',
        end: 'createEnd'
      }, {
        attr: 'arrivalTime',
        start: 'arrivalStartTime',
        end: 'arrivalEndTime'
      }];
    },
    chargeReportDateNames() {
      return [
        {
          attr: 'intoAccountTime',
          start: 'intoAccountStart',
          end: 'intoAccountEnd',
          endType: 1
        },
        {
          attr: 'chargeTime',
          start: 'chargeStart',
          end: 'chargeEnd',
          endType: 1
        },
        {
          attr: 'placeOrderTime',
          start: 'placeOrderStart',
          end: 'placeOrderEnd',
          endType: 1
        },
        {
          attr: 'sealingMonth',
          start: 'sealingMonthStart',
          end: 'sealingMonthEnd',
          endType: 1
        },
        {
          attr: 'sealMonth',
          start: 'sealMonthStart',
          end: 'sealMonthEnd',
          endType: 1
        }
      ];
    },
    sealingStateOption() {
      return [
        { value: -1, label: '全部' },
        { value: 0, label: '待封账' },
        { value: 1, label: '已封账' },
        { value: 2, label: '封账中' }
      ];
    },
    clientTypeOption() {
      return [{ value: 0, label: '公司' }, { value: 1, label: '个人' }];
    },
    flowTypes() {
      return [{ value: 0, label: '普通' }, { value: 1, label: '对抵' }];
    },
    verificationState() {
      return [{
        status: 0,
        verificationColor: '#31CC9C'
      }, {
        status: 1,
        verificationColor: '#406EFF'
      }, {
        status: 2,
        verificationColor: '#FD8546'
      }, {
        status: 3,
        verificationColor: '#FB5227'
      }];
    }
  },
  methods: {
    // 初始化金额、标题和弹窗类型
    setDrawerType(type) {
      switch (type) {
        case RETURN_TYPE.TRADE_AMOUNT:
          this.dialogModel = REPORTS_MODEL.CHANNEL;
          this.title = '交易总额明细';
          this.amountText = '含税金额';
          this.exportApi = EXPORT_API.ORDER;
          break;
        case RETURN_TYPE.WRITE_OFF_AMOUNT:
          this.dialogModel = REPORTS_MODEL.CHARGE_REPORT;
          this.title = '核销总额明细';
          this.amountText = '核销金额';
          this.exportApi = EXPORT_API.CHARGE_REPORT;
          break;
        case RETURN_TYPE.WRITE_OFF_FLOW:
          this.dialogModel = REPORTS_MODEL.FINANCE;
          this.title = '可核销流水明细';
          this.amountText = '可核销金额';
          this.exportApi = EXPORT_API.FINANCE;
          break;
        case RETURN_TYPE.PENDING:
          this.dialogModel = REPORTS_MODEL.CHANNEL;
          this.title = '待核销金额明细';
          this.amountText = '待核销金额';
          this.exportApi = EXPORT_API.ORDER;
          break;
        case RETURN_TYPE.THIRD_PARTY:
          this.dialogModel = REPORTS_MODEL.CHARGE_REPORT;
          this.title = '第三方代付核销明细';
          this.amountText = '核销金额';
          this.exportApi = EXPORT_API.CHARGE_REPORT;
          break;
        case RETURN_TYPE.PERSONAL:
          this.dialogModel = REPORTS_MODEL.CHARGE_REPORT;
          this.title = '个人核销公司明细';
          this.amountText = '核销金额';
          this.exportApi = EXPORT_API.CHARGE_REPORT;
          break;
        case RETURN_TYPE.ARRIVAL:
          this.dialogModel = REPORTS_MODEL.CHARGE_REPORT;
          this.title = '到账分布月份明细';
          this.amountText = '核销金额';
          this.exportApi = EXPORT_API.CHARGE_REPORT;
          break;
        case RETURN_TYPE.OVERDRAFT:
          this.dialogModel = REPORTS_MODEL.CHANNEL;
          this.title = '欠款分布月份明细';
          this.amountText = '待核销金额';
          this.exportApi = EXPORT_API.ORDER;
          break;
      }
    },
    // 渠道订单-所属部门
    queryDeptTreeList() {
      getDepartmentAll().then(res => {
        this.deptTreeList = (res && res.data) || [];
      }).catch(_ => {});
    },
    // 渠道订单-下单平台
    platformList() {
      platformList().then(res => {
        res && res.data.forEach((i) => {
          i.label = i.value;
          i.value = i.id;
        });
        this.orderPlatformOption = res.data;
      });
    },
    // 财务流水-自定义列数据
    getSettingData(showLoading = false) {
      if (showLoading) this.listLoading = true;
      getOrderSetting().then(res => {
        if (showLoading) this.listLoading = false;
        if (res.code === 200 && res.data !== '' && res.data.finance) {
          this.financeListSetting = this.jsonParse(res.data.finance);
        } else {
          this.financeListSetting = _.cloneDeep(this.initFinanceList);
        }
      }).catch(err => {
        this.financeListSetting = _.cloneDeep(this.initFinanceList);
        console.log(err);
      });
    },
    jsonParse(str) {
      if (typeof str !== 'string') return str;
      try {
        return JSON.parse(str);
      } catch (error) {
        console.log(error);
        return JSON.parse(JSON.stringify(this.initFinanceList));
      }
    },
    // 财务流水/核销记录-付款方式
    getPaySelect() {
      getPayOption().then(res => {
        if (res.code === 200) {
          res.data.forEach(i => {
            i.value = i.id;
            i.label = i.name;
          });
          if (this.dialogModel === REPORTS_MODEL.CHARGE_REPORT) {
            res.data.unshift({ value: -1, label: '全部' });
          }
          this.payOption = res.data;
        }
      });
    },
    // 财务流水-部门
    getDepartment() {
      getDepartmentList({ type: 3 }).then(res => {
        res.data.unshift({ value: -2, label: '--' });
        this.departmentList = res.data;
      }).catch(e => {
        console.log(e);
      });
    }
  }
};
